package com.netty.bfport.http.controller;

import com.alibaba.fastjson.JSONObject;
import com.netty.bfport.http.common.base.BaseResponse;
import com.netty.bfport.http.common.base.BaseResponseVO;
import com.netty.bfport.http.common.es.EsUtils;
import com.netty.bfport.http.domain.dto.UserLoginDTO;
import com.netty.bfport.http.service.UserMsgService;
import com.netty.bfport.http.service.UserService;
import com.netty.bfport.netty.server.WebTimeHandler;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: zhaoyang05
 * @Data: 2022/3/30 17:17
 * @Description
 */
@RestController
public class UserController {

    @Resource
    UserMsgService userMsgService;
    @Resource
    UserService userService;
    @Resource
    WebTimeHandler webTimeHandler;
    @Resource
    EsUtils esUtils;


    /**
     * 修改头像，性别，昵称，出生日期
     * */
    @PutMapping("/info")
    public BaseResponseVO updateUserInfo(@RequestBody UserLoginDTO userLoginDTO, HttpServletRequest request){
        String userId = getUserId(request).getString("id");
        return userMsgService.updateUserInfo(userLoginDTO, userId)==1? BaseResponse.setResultSuccess():BaseResponse.setResultError();
    }

    /**
     * 查询头像，性别，昵称，出生日期
     * */
    @GetMapping("/info")
    public BaseResponseVO findUserInfo(HttpServletRequest request){
        String userId = getUserId(request).getString("id");
        UserLoginDTO userInfo = userMsgService.findUserInfo(userId);
        return BaseResponse.setResultSuccess(userInfo);
    }

    private JSONObject getUserId(HttpServletRequest request){
        String token = request.getHeader("token");
        token = token.replaceAll("#","\r\n");
        String s = StringEscapeUtils.unescapeJava(token);
        //转义/r/n
        JSONObject msg = userService.tokenAnalysis(s);
        return msg;
    }


    @GetMapping("/web/task")
    public void asdsa(@RequestParam String cron){
        webTimeHandler.webMsgPushPub(cron);
    }
}
